package com.example.demo1.controller;


import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jdk.jshell.spi.ExecutionControl;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-01-10
 * Time: 12:46
 */
@RestController
@RequestMapping("/user")
public class IndexController {
    @PostMapping("/login")
    public Boolean login(String userName, String password, HttpSession session) {
        //userName和passWord为必传参数，可以用RequestParam，也可以写if判断
        /*if(userName == null || "".equals(userName)) {
            return false;
        }*/
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }

        if("admin".equals(userName) && "admin".equals(password)) {
            session.setAttribute("userName",userName);
            return true;
        }

        return false;
    }

    @GetMapping("/getLoginUser")
    public String getLoginUser(HttpServletRequest request){
        HttpSession session = request.getSession();
        if(session.getAttribute("userName") != null) {
            return (String) session.getAttribute("userName");
        }
        return "";
    }
}
